package com.zm.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zm.jdbc.AccessDb;


public class LoginServlet extends HttpServlet {
    private AccessDb db;
	private Connection conn=null;
	private PreparedStatement stat=null;
	private ResultSet set=null;
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		
	ServletContext application=config.getServletContext();
		 if(application.getAttribute("accessdb")==null){
			 db=new AccessDb();
			 application.setAttribute("accessdb",db);
		  }
	 try{
			 
		 conn=((AccessDb)application.getAttribute("accessdb")).getConnection();
	     stat=conn.prepareStatement("SELECT * FROM account WHERE name=?");
	     
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
	}
	
	
//	
//	public boolean isAccountExist(HttpServletRequest request,HttpServletResponse response) {
//		
//		String name=request.getParameter("name");
//		byte[] temp;
//		try {
//			temp = name.getBytes("ISO-8859-1");
//			name=new String(temp,"gbk");
//			return db.hasName(name);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			
//		}
//		return false;
//		
//			
//	}
//	
//	
//	public void login(HttpServletRequest request,HttpServletResponse response){
//		try {
//		    RequestDispatcher dispatcher=null; 
//			String name=request.getParameter("name");
//			String passwd=request.getParameter("passwd");
//			byte[] temp=name.getBytes("ISO-8859-1");
//			name=new String(temp,"gbk");
//			stat.setString(1,name);
//			set=stat.executeQuery();
//			if(set.next()){
//			
//				String passwdTemp=set.getString("passwd");
//				if(passwdTemp.equals(passwd)){
//				    HttpSession session=request.getSession(true);
//				    session.setAttribute("name", name);
//					dispatcher=request.getRequestDispatcher("../AccessDb.jsp");
//					dispatcher.forward(request, response);	
//				}else {
//					 HttpSession session=request.getSession(true);
//					   session.setAttribute("errorLogin",1);
//					   session.setAttribute("errorUser",name);
//					   response.sendRedirect("../Login.jsp");
//					//request.setAttribute("error",1);
//					//dispatcher=request.getRequestDispatcher("../FailPage.jsp");
//					//dispatcher.forward(request, response);	
//				}
//			}else{
//			   HttpSession session=request.getSession(true);
//			   session.setAttribute("errorLogin",2);
//			   session.setAttribute("errorUser",name);
//			   response.sendRedirect("../Login.jsp");
//                //	request.setAttribute("error",2);
//                //  dispatcher=request.getRequestDispatcher("../FailPage.jsp");
//                //	dispatcher.forward(request, response);	
//			}
//			
//			
//			
//			
//
//			
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ServletException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//			
//		
//		
//		
//		
//	}
//	
//	
//	public void service(HttpServletRequest request,HttpServletResponse response){
//		
//		String flag=request.getParameter("flag");
//		if("isExist".equals(flag)){
//			if(!isAccountExist(request,response)){
//				try {
//					PrintWriter out=response.getWriter();
//					out.println("noexist");
//					out.flush();
//					out.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//				
//			}
//		}else if("login".equals(flag)){
//			
//			login(request,response);
//		}
//	}
	public void service(HttpServletRequest request,HttpServletResponse response){
		
		try {
		    RequestDispatcher dispatcher=null; 
			String name=request.getParameter("name");
			String passwd=request.getParameter("passwd");
			byte[] temp=name.getBytes("ISO-8859-1");
			name=new String(temp,"utf-8");
			stat.setString(1,name);
			set=stat.executeQuery();
			if(set.next()){
			
				String passwdTemp=set.getString("passwd");
				if(passwdTemp.equals(passwd)){
				
					db.changeStatus("logined", name);
				    HttpSession session=request.getSession(true);
				    session.setAttribute("Account", name);
				    session.setAttribute("Login", "logined");
			        response.sendRedirect("../TagTst.jsp");
					
				}else {
//					 HttpSession session=request.getSession(true);
//					   session.setAttribute("errorLogin",1);
//					   session.setAttribute("errorUser",name);
//					   response.sendRedirect("../Login.jsp");
					request.setAttribute("error",1);
					dispatcher=request.getRequestDispatcher("../LoginJsp.jsp");
					dispatcher.forward(request, response);	
				}
			}else{
//			   HttpSession session=request.getSession(true);
//			   session.setAttribute("errorLogin",2);
//			   session.setAttribute("errorUser",name);
//			   response.sendRedirect("../Login.jsp");
                	request.setAttribute("error",2);
                    dispatcher=request.getRequestDispatcher("../LoginJsp.jsp");
                	dispatcher.forward(request, response);	
			}
			
			
			
			

			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}
	
	
	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	

}
